{include file="public/header" /}
<link href="/static/layui/css/layui.css" rel="stylesheet">
<script src="/static/layui/layui.js" charset="utf-8"></script>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <!-- Panel Other -->
    <div class="ibox float-e-margins">
        <div class="ibox-title" style="position:relative;">
            <h5>分类列表</h5>
            <h5 style="position:absolute;right:10px;top:5px;padding:10px;margin:0px;background:#1ab394;color:#fff;border-radius
            :5px;cursor:pointer;" onclick="balabceAll();">批量结算</h5>
        </div>
        <div class="ibox-content">
            <!--搜索框开始-->
            <div class="row">
                <div class="col-sm-12">
                    <form name="admin_list_sea" class="form-search" method="post" action="{:url('lookOrder')}">
                        <input type="hidden" name="uid" value="{$data.uid}">
                        <input type="hidden" name="goods_id" value="{$data.goods_id}">
                        <div class="col-sm-3" style="width:100%;">
                            <div class="input-group">
                                <select class="form-control m-b chosen-select" name="status" id='status' style="width:25%;">
                                    <option value="3">选择订单状态</option>
                                    <option value="0">未结算</option>
                                    <option value="1">待结算</option>
                                    <option value="2">已结算</option>
                                </select>
                                <span class="input-group-btn">
                                    <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i> 搜索</button>
                                </span>
                            </div>
                        </div>
                    </form>
                </div>
                <!--搜索框结束-->
                <div class="hr-line-dashed"></div>
                <div class="example-wrap">
                    <div class="example">
                        <table class="table table-bordered table-hover">
                            <thead>
                                <tr class="long-tr">
                                    <th>
                                      <div class="layui-unselect layui-form-checkbox check-all" lay-skin="primary" data-id="2"><i class="layui-icon"></i></div>
                                    </th>
                                    <th>ID</th>
                                    <th>用户</th>
                                    <th>订单号</th>
                                    <th>商品id</th>
                                    <th>券后价</th>
                                    <th>佣金</th>
                                    <th>创建时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody id="list-content">
                            {volist name="data.orders" id="vo"}
                                <tr class="long-td">
                                    <td>
                                      <div class="layui-unselect layui-form-checkbox check-style" lay-skin="primary" order_id="{$vo.id}" data-id="2"><i class="layui-icon"></i></div>
                                    </td>
                                    <td>{$vo.id}</td>
                                    <td>{$vo.user.username}</td>
                                    <td>{$vo.trade_id}</td>
                                    <td>{$vo.goods.sp_id}</td>
                                    <td>{$vo.goods.last_price}</td>
                                    <td>{$vo.goods.money*$vo.goods.commision/100}</td>
                                    <td>{$vo.create_time|date="Y-m-d H:i:s",###}</td>
                                    <td>
                                    {switch name="vo.status"}
                                        {case value="0"}
                                            <a href="javascript:;" class="btn btn-primary btn-xs"><i class="fa fa-paste"></i> 待结算</a>&nbsp;&nbsp;
                                        {/case}
                                        {case value="1"}
                                            <a href="javascript:;" onclick="balance(this,{$vo.id})" class="btn btn-primary btn-xs"><i class="fa fa-paste"></i> 结算</a>&nbsp;&nbsp;
                                        {/case}
                                        {case value="2"}
                                            <a href="javascript:;" class="btn btn-primary btn-xs"><i class="fa fa-paste"></i> 已结算</a>&nbsp;&nbsp;
                                        {/case}
                                    {/switch}
                                    </td>
                                </tr>
                            {/volist}
                            </tbody>
                        </table>
                        {$data.page}
                    </div>
                </div>
                <!-- End Example Pagination -->
            </div>
        </div>
    </div>
<!-- End Panel Other -->
</div>

{include file="public/footer" /}


<script type="text/javascript">
    var uid = {$data.uid};

    function balance(obj,id) {
        var order_ids = new Array();
        order_ids[order_ids.length] = id;
        balanceAllOrder(order_ids);
    }

</script>
<script type="text/javascript">
    var goods_id = {$data.goods_id};
    var aLi=document.getElementsByClassName("check-style");
    var lab = document.getElementsByClassName("check-style");
    function ch(){
        var cc = $(".check-all").hasClass("layui-form-checked"); //false
        var checkLab = [];
        if (cc) {
            for (var i = 0; i < lab.length; i++) {
                const e = lab[i];
                checkLab.push(lab[i]);
                lab[i].classList.add("layui-form-checked");
                $(".check-all").addClass("layui-form-checked")
            }
        }else{
            for (var i = 0; i < lab.length; i++) {
                const e = lab[i];
                checkLab.push(lab[i]);
                lab[i].classList.remove("layui-form-checked");
                $(".check-all").removeClass("layui-form-checked")
            }
        }
    }

    $(".check-all").click(function(){
        if($(".check-all").hasClass("layui-form-checked")){
            $(".check-all").removeClass("layui-form-checked")
        }else{
            $(".check-all").addClass("layui-form-checked")
        }
        ch();
    })

    // 单个选中
    $(".check-style").on("click", function () {
        $(this).toggleClass("layui-form-checked")
        if(!($(this).hasClass("layui-form-checked"))){
            $(".check-all").removeClass("layui-form-checked")
        }else{
            var onOff=true
            for (var i = 0; i < lab.length; i++) {
                const e = lab[i];
                if(lab[i].classList.contains('layui-form-checked')==false){
                    onOff=false;
                    return;
                    // 若剩下的元素中有一个为假，则说明全选按钮不能被选中
                }
            }
            if(onOff){
                $(".check-all").addClass("layui-form-checked");
            }
        }
    })

    //点击批量结算获取所有选中的订单
    function balabceAll() {
        var order_ids  = new Array();
        $(".layui-form-checked").each(function(index, element) {
            var order_id = $(this).attr('order_id');
            if(order_id && order_id != undefined){
                order_ids[order_ids.length] = order_id;
            }
        });
        balanceAllOrder(order_ids);
    }

    //结算
    function balanceAllOrder(order_arr) {
        layui.use('layer', function(){
            var layer = layui.layer;
            //询问框
            layer.confirm('您确定要结算'+order_arr.join(',')+'？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                $.post('{:url("balanceOrder")}',{'ids':order_arr.join(','),'uid':uid,'goods_id':goods_id},function(data){
                    if(data.code == 1){
                        layer.msg(data.msg, {icon: 1,time:1500,shade: 0.1},function(index){
                            window.location.reload();
                        });
                    }else if(data.code == -1){
                      layer.msg(data.result+'不能结算', {icon: 2,time:1500,shade: 0.1});
                    }else{
                      layer.msg(data.msg, {icon: 2,time:1500,shade: 0.1});
                    }
                },'json');
            }, function(){

            });
        });
    }
</script>
</body>
</html>
